<?php

namespace Database\Seeders;

use App\Models\Manager\Dictionary;
use App\Models\Manager\Group;
use App\Models\Manager\Permission;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class InitializeSystem extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        if (User::all()->find(1) === null) {
            //初始化数据字典
            DB::table('dictionary')->insert([
                [
                    'type' => '权限分类',
                    'name' => '用户权限'
                ],
                [
                    'type' => '权限分类',
                    'name' => '组权限'
                ],
            ]);
            //初始化权限
            $defaultUserPermission = Config::get('defaultpermission.user');
            foreach ($defaultUserPermission as $key => $value) {
                Permission::create([
                    'permissionName' => $key,
                    'description' => '系统自动创建的用户默认权限',
                    'categoryId' => 1
                ]);
            }
            $defaultGroupPermission = Config::get('defaultpermission.group');
            foreach ($defaultGroupPermission as $key => $value) {
                Permission::create([
                    'permissionName' => $key,
                    'description' => '系统自动创建的组默认权限',
                    'categoryId' => 2
                ]);
            }

//          初始化 root
            $user = User::create(
                [
                    'userName' => 'root',
                    'password' => Hash::make('123456'),
                    'nickName' => 'root',
                    'email' => 'root@qq.com',
                    'tel' => '12345678910',
                ]
            );
            //初始化root组
            $group = $user->Group()->create(
                [
                    'name' => 'root',
                    'description' => '超级管理员组'
                ]
            );
            //添加系统组
            DB::table('group')->insert([
                [
                    'name' => '组创建者',
                    'description' => '用户创建的组的创建者角色'
                ],
                [
                    'name' => '组管理员',
                    'description' => '用户创建的组的管理员角色'
                ]
            ]);
            //设置root组的管理员为root
            $group->GroupManager()->create([
                'roleId' => 2,
                'userId' => $user['id']
            ]);

            //初始化用户权限
            {
                $UserPermissions = [];
                foreach ($defaultUserPermission as $key => $value) {
                    $UserPermissions[] = [
                        "userId" => $user['id'],
                        "permissionId" => $value
                    ];
                }
                DB::table('users_permissions')->insert($UserPermissions);
            }
            //初始化组权限
            {
                $groupPermissions = [];

                foreach ($defaultGroupPermission as $value) {
                    $groupPermissions[] = [
                        'groupId' => $group['id'],
                        'permissionId' => $value
                    ];
                }
                DB::table('group_permission_relation')->insert($groupPermissions);
            }
        }
    }
}
